import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wangxin
 * Date: 2024-12-30
 * Time: 23:00
 */
public class MyStack<E> {
    public Object[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new Object[10];
    }

    public void push(E val) {
        if(isFull()) {
            //扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    public E pop() {
        if(empty()) {
            return null;
        }
        E oldVal = (E)elem[usedSize-1];
        usedSize--;
        return oldVal;
    }

    public E peek() {
        if(empty()) {
            return null;
        }
        return (E)elem[usedSize-1];
    }

    public boolean empty() {
        return usedSize == 0;
    }
    public int size(){
        return usedSize;
    }
}
